<?php
$db = new Database;
$auth = Auth::getAuth('current_user');
$user_id = $auth['id'];
$company_id = $auth['company_id'];
$user_groupType = $auth['role_type'];
$user_group = $auth['role'];
$userLevel = $auth['userlevel_id'];
if(isset($_POST['action'])){
    if($_POST['action']=="updateAnnouncement"){
        $announcement = $_POST['announcement'];
        $update = array(
            "is_active"=>0
            );
        $condition = array(
            "company_id"=>$company_id
            );
        $db->update("tbannouncement",$update,$condition);
        $insertAnnoun = array(
            "company_id"=>$company_id,
            "announcement"=>$announcement,
            "is_active"=>1,
            "date"=>""
        );
        $db->insert("tbannouncement",$insertAnnoun);
    }elseif($_POST['action']=="getApproverWorflow"){
        $value = $_POST['value'];
        if($value==1){
            ?>
            <option value="head" class="pos_group">Head</option>
            <option value="asst_head" class="pos_group">Assistant Head</option>
            <?php
        }elseif($value==2){
            $getPosition = $db->query("SELECT * FROM tbposition WHERE company_id='$company_id' and is_active=1","array");
            $countPosition = $db->query("SELECT * FROM tbposition WHERE company_id='$company_id' and is_active=1","numrows");
            if($countPosition>0){
                foreach($getPosition as $setPosition){
                    ?>
                    <option class="pos_company"><?php echo $setPosition['position']; ?></option>
                    <?php
                }
            }else{
                ?>
                <option class="pos_company" value="0">-No Position-</option>
                <?php
            }
        }else if($value==3){
            $queryUsers = $db->query("SELECT * from tbuser where status = 1 and company_id = '$company_id' and is_active = 1","array");
            foreach($queryUsers as $dataUsers){
                ?>
                <option class="specific_name"><?php echo setName($dataUsers['firstname'],"",$dataUsers['lastname']); ?></option>
                <?php
            }
        }else if($value==0){
            ?>
            <option value="0">--Select--</option>
            <?php
        }
    }elseif($_POST['action']=="getStatusWorflow"){
        ?>
        <option value="review" class="workFlowStat">For Review</option>
        <option value="approval" class="workFlowStat">For Approval</option>
        <?php
    }else if($_POST['action']=="addPosition"){
        $position = $_POST['position'];
        $getMatchPosition = $db->query("SELECT * FROM tbposition WHERE position = '$position' and company_id='$company_id' and is_active=1","numrows");
        if($getMatchPosition>=0){
            $insertAnnoun = array(
                "company_id"=>$company_id,
                "position"=>$_POST['position'],
                "is_active"=>1
            );
            $db->insert("tbposition",$insertAnnoun);
            echo 1;    
        }else{
            echo 0;  
        }
    }else if($_POST['action']=="getApproverTypeAdminSett"){
        $value = $_POST['value'];
        $type = $_POST['type'];
        if($value==1){
            //Department , Role Type
            $getActiveDept = $db->query("SELECT * FROM tborgchart o LEFT JOIN tborgchartobjects oo on o.id=oo.orgChart_id where o.status = 1 and o.is_active = 1 and o.company_id ='$company_id'","array");
            ?>
            <div class="fields">
                <div class="label fontGray" id="labelObject">Department:</div>
                <div class="input_position">
                <select class="input-x-medium approverTypeAdminSett<?php echo $type; ?> <?php echo $type; ?>" rel="role">
                <?php
                foreach($getActiveDept as $setActiveDept){
                    ?>
                    <option value="<?php echo $setActiveDept['role']; ?>"><?php echo $setActiveDept['role']; ?></option>
                    <?php
                }
                ?>
                </select>
                </div>
            </div>
            <div class="fields">
                <div class="label fontGray" id="labelObject">Role Type:</div>
                <div class="input_position">
                    <select class="input-x-medium roleTypeAdminSett<?php echo $type; ?> <?php echo $type; ?>" rel="role">
                        <option value="1" class="pos_group">Head</option>
                        <option value="2" class="pos_group">Assistant Head</option>
                    </select>
                </div>
            </div>
            
            <?php
        }else if($value==2){
            $queryUsers = $db->query("SELECT * from tbuser where status = 1 and company_id = '$company_id' and is_active = 1","array");
            ?>
            <label>Specific User:</label>
            <select class="specific_name <?php echo $type; ?>" rel="specificUser">
            <?php
            foreach($queryUsers as $dataUsers){
                ?>
                <option value="<?php echo setName($dataUsers['firstname'],"",$dataUsers['lastname']); ?>"><?php echo setName($dataUsers['firstname'],"",$dataUsers['lastname']); ?></option>
                <?php
            }
            ?>
            </select>
            <?php
        }
    }else if($_POST['action']=="moveRequest"){
        $errorUser = 0;
        $errorPendingReq = 0;
        $pendingRequest_id = array();
        $requestFlow = array();
        $requestFlowObj_id = array();
        $wf_master_id = array();
        $request_id = array();
        //get
        if($_POST['getUserType']==1){
            //echo "Per Department";
            $getUsersArray = explode("_",$_POST['getUser']);
            $getUserDb = $db->query("SELECT * FROM tbuser where role_type='".$getUsersArray[0]."' and role='".$getUsersArray[1]."' and company_id='$company_id'","row");
            $getUsersPerName =  $getUserDb['firstname']." ".$getUserDb['lastname'];
            $getUsersPerDept =  tempGroupTypeReverse($getUsersArray[0])."_".$getUsersArray[1];
            $getUserRole = $getUsersArray[1];
            $getUserRoleType = $getUsersArray[0];
            $getUser_id = $getUserDb['id'];
        }else if($_POST['getUserType']==2){
            //echo "Per User";
            $getUserDb = $db->query("SELECT * FROM tbuser where concat_ws(' ',firstname,lastname) = '".$_POST['getUser']."' and company_id='$company_id'","row");
            $getUsersPerName = $_POST['getUser'];
            $getUserRole = $getUserDb['role'];
            $getUserRoleType = $getUserDb['role_type'];
            $getUsersPerDept = tempGroupTypeReverse($getUserRoleType)."_".$getUserRole;
            $getUser_id = $getUserDb['id'];
        }
        //echo $getUsersPerName." or ".$getUsersPerDept;
        $getUsersPerDeptArray = explode("_",$getUsersPerDept);
        $getUserRows = $db->query("SELECT * FROM tbuser where concat_ws(' ',firstname,lastname) = '$getUsersPerName' or (role_type='".$getUsersPerDeptArray[0]."' and role='".$getUsersPerDeptArray[1]."') and status = 1 and company_id='$company_id' and is_active = 1","numrows");
        if($getUserRows>0){
            $getPendingRequestRows = $db->query("SELECT * FROM tbrequest r LEFT JOIN tbrequest_process rr on r.id=rr.request_id where r.company_id = '$company_id' and rr.status = 0 and (rr.approver='$getUsersPerName' or rr.approver='$getUsersPerDept')","numrows");
            $getPendingRequest = $db->query("SELECT rr.wf_master_id as wf_master_id, r.requestFlowObj_id as requestFlowObj_id, r.requestFlowApprover as requestFlow, rr.request_id as request_id,rr.id as rr_id FROM tbrequest r LEFT JOIN tbrequest_process rr on r.id=rr.request_id where r.company_id = '$company_id' and rr.status = 0 and (rr.approver='$getUsersPerName' or rr.approver='$getUsersPerDept')","array");
            if($getPendingRequestRows>0){
                foreach($getPendingRequest as $dataPendingRequest){
                    //echo "Request_id: ".$dataPendingRequest['request_id']." Request process_id: ".$dataPendingRequest['rr_id']."\n";
                    array_push($pendingRequest_id,$dataPendingRequest['rr_id']);
                    array_push($requestFlow,$dataPendingRequest['requestFlow']);
                    array_push($requestFlowObj_id,$dataPendingRequest['requestFlowObj_id']);
                    array_push($wf_master_id,$dataPendingRequest['wf_master_id']);
                    array_push($request_id,$dataPendingRequest['request_id']);
                }
            }else{
                //echo "No Pending Request";
                $errorPendingReq++;
            }
        }else{
            //echo "No User in the position";
            $errorUser++;
        }
        
        if($_POST['setUserType']==1){
            //echo "Per Department";
            $setUsersArray = explode("_",$_POST['setUser']);
            $setUserDb = $db->query("SELECT * FROM tbuser where role_type='".$setUsersArray[0]."' and role='".$setUsersArray[1]."' and company_id='$company_id'","row");
            $setUsersPerName =  $setUserDb['firstname']." ".$setUserDb['lastname'];
            $setUsersPerDept =  tempGroupTypeReverse($setUsersArray[0])."_".$setUsersArray[1];
            $setUserRole = $setUsersArray[1];
            $setUserRoleType = $setUsersArray[0];
            $setUser_id = $setUserDb['id'];
        }else if($_POST['setUserType']==2){
            //echo "Per User";
            $setUserDb = $db->query("SELECT * FROM tbuser where concat_ws(' ',firstname,lastname) = '".$_POST['setUser']."' and company_id='$company_id'","row");
            $setUsersPerName = $_POST['setUser'];
            $setUserRole = $setUserDb['role'];
            $setUserRoleType = $setUserDb['role_type'];
            $setUsersPerDept = tempGroupTypeReverse($setUserRoleType)."_".$setUserRole;
            $setUser_id = $setUserDb['id'];
        }
        //echo $getUsersPerName." or ".$getUsersPerDept;
        $setUsersPerDeptArray = explode("_",$setUsersPerDept);
        $setUserRows = $db->query("SELECT * FROM tbuser where concat_ws(' ',firstname,lastname) = '$setUsersPerName' or (role_type='".$setUsersPerDeptArray[0]."' and role='".$setUsersPerDeptArray[1]."') and status = 1 and company_id='$company_id' and is_active = 1","numrows");
        if($setUserRows>0){
            //echo $setUserRows;
        }else{
            //echo "No User in the position";
            $errorUser++;
        }
        
        /**************/
        if($errorUser>0){
            echo "Error User";
        }else{
            if($getUser_id!=$setUser_id){
                $getActiveOrgChart = $db->query("SELECT id FROM tborgchart where is_active = 1 and company_id='$company_id' and status=1","row");
                $getObjectID = $db->query("SELECT object_id from tborgchartobjects where role='$getUserRole' and orgChart_id='".$getActiveOrgChart['id']."'","row");
                $errorChild = 0;
                $d = 0;
                while(true){
                    //FOR WORKFLOW ROLE
                    if($d==0){
                        $master = $db->query("SELECT child_id FROM tborgchartline where master_id = '".$getObjectID['object_id']."' and orgChart_id = '".$getActiveOrgChart['id']."'","row");    
                    }else{
                        $master = $db->query("SELECT child_id FROM tborgchartline where master_id = '".$master['child_id']."' and orgChart_id = '".$getActiveOrgChart['id']."'","row");    
                    }
                    $queryMasterNodeOrgChart = $db->query("SELECT * FROM tborgchartobjects where object_id='".$master['child_id']."' and orgChart_id = '".$getActiveOrgChart['id']."'","row");
                    if($master['child_id']==""){
                        break;
                    }else{
                        if($queryMasterNodeOrgChart['role']==$setUserRole){
                            $errorChild++;
                            break;   
                        }
                    }
                    $d++;
                }
                if($getUserRole==$setUserRole && $getUserRoleType==1 && $setUserRoleType = 2){
                    $errorChild++;
                }
                /*if($errorChild>0){
                    echo "Mas mababa ung role";
                }else{
                    
                }*/
                if($errorPendingReq>0){
                    echo "Error Pending";
                }else{
                    for($a=0;$a<count($pendingRequest_id);$a++){
                        if($pendingRequest_id!=""){
                            $update = array(
                                "approver"=>setApprover($_POST['setUser'])
                                );
                            $where = array(
                                "id"=>$pendingRequest_id[$a]
                                );
                            $db->update("tbrequest_process",$update,$where);
                            $roleApprover = explode("<->",$requestFlow[$a]);
                            $roleApproverObj_id = explode("<->",$requestFlowObj_id[$a]);
                            $index = 0;
                            $return = "";
                            $updates = 0;
                            $inc = 0;
                            $getActiveOrgChart = $db->query("SELECT id FROM tborgchart where is_active = 1 and company_id='$company_id' and status=1","row");
                            $getSetRole = explode("_",setApprover($_POST['setUser']));
                            $getObjectID = $db->query("SELECT object_id from tborgchartobjects where role='".$getSetRole[1]."' and orgChart_id='".$getActiveOrgChart['id']."'","row");
                            $arrayWorkflowRole = array();
                            $arrayOrgchart = array();
                            foreach($roleApprover as $setroleApprover){
                                if($setroleApprover!=""){
                                    if($roleApproverObj_id[$index]==$wf_master_id[$a]){
                                        $return=setApprover($_POST['setUser'])."<!>";
                                        $getApproverRole = explode("_",setApprover($_POST['setUser']));
                                        array_push($arrayWorkflowRole,$getApproverRole[0]);
                                        array_push($arrayOrgchart,$getApproverRole[1]);
                                        $updates++;
                                    }else{
                                        if($updates>0){
                                            //$return=$setroleApprover."<-->";
                                            $getApproverRole = explode("_",$setroleApprover);
                                            if($getApproverRole[0]=="Head" || $getApproverRole[0]=="Assistant Head"){
                                                if($inc==0){
                                                    $masterOrgChart = $db->query("SELECT master_id FROM tborgchartline where child_id = '".$getObjectID['object_id']."' and orgChart_id = '".$getActiveOrgChart['id']."'","row");
                                                    
                                                }else{
                                                    $masterOrgChart = $db->query("SELECT master_id FROM tborgchartline where (child_id = '".$masterOrgChart['master_id']."' || child_id='".$masterOrgChart['object_id']."') and orgChart_id = '".$getActiveOrgChart['id']."'","row");
                                                }
                                                $queryMasterNodeOrgChart = $db->query("SELECT * FROM tborgchartobjects where object_id='".$masterOrgChart['master_id']."' and orgChart_id = '".$getActiveOrgChart['id']."'","row");
                                                $dept = $queryMasterNodeOrgChart['role'];
                                                $return = $getApproverRole[0]."_".$dept;
                                                array_push($arrayWorkflowRole,$getApproverRole[0]);
                                                array_push($arrayOrgchart,$dept);
                                                $inc++;
                                            }else{
                                                array_push($arrayWorkflowRole,$setroleApprover);
                                                array_push($arrayOrgchart,"");
                                            }
                                        }else{
                                            $getApproverRole = explode("_",$setroleApprover);
                                            if($getApproverRole[0]=="Head" || $getApproverRole[0]=="Assistant Head"){
                                                array_push($arrayWorkflowRole,$getApproverRole[0]);
                                                array_push($arrayOrgchart,$getApproverRole[1]);
                                            }else{
                                                array_push($arrayWorkflowRole,$setroleApprover);
                                                array_push($arrayOrgchart,"");
                                            }
                                        }
                                    }
                                    $index++;   
                                }
                            }
                            //array
                            $minus=0;$roleToInserts = "";$object_id2 = "";$lastApproverCount=0;$getRowsApprover = 0;$confirmation=0;$confirm = 0;$proceedToNext = 0;$approver = "";
                            for($o=0;$o<count($arrayWorkflowRole);$o++){
                                if($arrayWorkflowRole[$o]=="Head" || $arrayWorkflowRole[$o]=="Assistant Head"){
                                    if($arrayWorkflowRole[$o]=="Head" and $arrayWorkflowRole[$o-1]=="Assistant Head"){
                                        $minus++;
                                    }
                                    $roleToInserts.= $arrayWorkflowRole[$o]."_".$arrayOrgchart[$o-$minus]."<->";
                                    $getRowsApprover = $db->query("SELECT * FROM tbuser where role_type='".tempGroupType($arrayWorkflowRole[$o])."' and role='".$arrayOrgchart[$o-$minus]."' and is_active=1 and status =1 and company_id = '$company_id'","numrows");
                                    //echo $roleToInserts."->".$getRowsApprover."\n";
                                    if($getRowsApprover>0){
                                        $approver = $arrayWorkflowRole[$o]."_".$arrayOrgchart[$o-$minus];
                                    }
                                }else{
                                    $roleToInserts.= $arrayWorkflowRole[$o]."<->";
                                    $getRowsApprover = $db->query("SELECT * FROM tbuser u LEFT JOIN tbposition p on u.position_id = p.id where (concat_ws(' ',u.firstname,u.lastname) = '".$roleToInserts."' or p.position = '".$roleToInserts."') and u.is_active=1 and u.status =1 and u.company_id = $company_id","numrows");
                                    //echo $roleToInserts."->".$getRowsApprover."\n";
                                    if($getRowsApprover>0){
                                       $approver = $arrayWorkflowRole[$o];
                                    }
                                }
                            }
                            //end array
                            $updateRequestFlow = array(
                                "requestFlowApprover"=>$roleToInserts
                            );
                            $whereRequestFlow = array(
                                "id"=>$request_id[$a]
                            );
                            $db->update("tbrequest",$updateRequestFlow,$whereRequestFlow);
                        }
                    }
                    echo 1;
                }
            }else{
                echo "Same User not Allowed";
            }
        }
    }else if($_POST['action']=="getNotification"){
        $getPosition = $db->query("SELECT * FROM tbposition WHERE id='".$auth['position_id']."' and company_id='$company_id' ","row");
        $getUpdatedRequest = $db->query("SELECT sf.numCol as numCol, sf.colValue_formID as colValue_formID,
                                   r.date as date_request,r.user_id as user_id, f.id as form_id,
                                rp.wf_master_id as wf_master_id, f.form_title as form_title, r.data as data, f.workflow_id as workflow_id,
                                r.id as request_id,rp.id as requestProcess_id, u.extension as extension FROM tbrequest r
                                LEFT JOIN tbrequest_process rp on r.id=rp.request_id
                                LEFT JOIN tbform f on f.id=r.form_id
                                LEFT JOIN tbset_field sf on sf.companyID=f.company_id
                                LEFT JOIN tbuser u on u.id = r.user_id
                                WHERE (".roleApprover($user_groupType,$user_group)."
                                or rp.approver='".$auth['firstname']." ".$auth['lastname'] ."' or rp.approver='".$getPosition['position']."')
                                AND rp.status=0 and r.status=0 and r.company_id = $company_id $sort ORDER BY r.id DESC","array");
        $countRequest = $db->query("SELECT sf.numCol as numCol, sf.colValue_formID as colValue_formID,
                                   r.date as date_request,r.user_id as user_id, f.id as form_id,
                                rp.wf_master_id as wf_master_id, f.form_title as form_title, r.data as data,
                                f.workflow_id as workflow_id,
                                r.id as request_id,rp.id as requestProcess_id FROM tbrequest r
                                LEFT JOIN tbrequest_process rp on r.id=rp.request_id
                                LEFT JOIN tbform f on f.id=r.form_id
                                LEFT JOIN tbset_field sf on sf.companyID=f.company_id
                                LEFT JOIN tbuser u on u.id = r.user_id
                                WHERE (".roleApprover($user_groupType,$user_group)."
                                or rp.approver='".$auth['firstname']." ".$auth['lastname'] ."' or rp.approver='".$getPosition['position']."')
                                AND rp.status=0 and r.status=0 and r.company_id = $company_id $sort","array");
        $queryRegRequest = $db->query("SELECT * from tbuser where status = 0 and company_id = '$company_id' and is_active = 1","array");
        $numrowsRegRequest = $db->query("SELECT * from tbuser where status = 0 and company_id = '$company_id' and is_active = 1","numrows");
        /*if($countRequest>0){
            
        }else{
            echo "<span class='fontGray'>No Request was submitted.</span>";
        }*/
        
        foreach($getUpdatedRequest as $request){
        ?>
            <div class="requestRow_<?php echo $request['request_id']; ?> requestBlock_<?php echo $request['request_id']; ?>" id="notiLi">
                <div style="float: left;margin-left: 5px;width: 165px">
                    <?php echo myAvatar($request['extension'],$request['user_id'],50,50,"none"); ?>
                    <div style="float: left;color: #000;margin-left: 5px;font-weight: bold;"><?php echo $request['form_title'];?></div><br />
                    <label class="fontGray timeago" style="margin-right:15px;font-size:10px;" title="<?php echo $request['date_request'];?>"></label><br />
                    <span class="requestButtonPost">
                    
                    
                        <?php
                        
                        $getButtonProperty = $db->query("SELECT * FROM tbworflow_obects wo LEFT JOIN tbworkflow w on wo.worflow_id = w.id where w.id='".$request['workflow_id']."' and wo.object_id='".$request['wf_master_id']."'","row");
                        //buttons
                        
                        $arraySplit = substr($getButtonProperty['buttonStatus'],0,strlen($getButtonProperty['buttonStatus'])-1);
                        $arraySplit = explode("+",$arraySplit);
                        $buttons.="<label class='viewUserForm pointer' style='color:#6D84B4;font-size:10px' id='viewUserForm_".$request['request_id']."_".$request['requestProcess_id']."' object-id='".$nextApprover."' request_status='".$requestStatus."' sequence-approver='".$request['wf_master_id']."' button-properies='".$getButtonProperty['buttonStatus']."'>View</label> | ";
                        for($a=0;$a<count($arraySplit);$a++){
                             $buttonID =  explode(":",$arraySplit[$a]);
                             $nextApprover = explode(">",$buttonID[1]);
                             $requestStatus = $nextApprover[1];
                             $buttonID = $buttonID[0];
                             $nextApprover = $nextApprover[0];
                             $getButton = $db->query("SELECT * FROM tbbuttons where id = $buttonID","row");
                             $buttons.="<label class='approverButton pointer' style='color:#6D84B4;font-size:10px' id='button_".$request['request_id']."_".$request['requestProcess_id']."' object-id='".$nextApprover."' request_status='".$requestStatus."' sequence-approver='".$request['wf_master_id']."'>".$getButton['button']."</label> | ";
                        }
                        echo substr($buttons,0,strlen($buttons)-2);
                        $buttons = "";
                        ?>
                        <!--button class="button-icon actionBtn save_form" title="Save Created Form"><li class="icon-file"></li></button>
                        <button class="button-icon actionBtn actionUserRequest" data-id="<?php echo $request['request_id']; ?>" rel="accept" id="accept_<?php echo $request['request_id'];?>_<?php echo $request['requestProcess_id']; ?>" object-id="<?php echo $request['wf_master_id'];?>" title="Accept"><li class="icon-ok"></li></button>
                        <button class="button-icon actionBtn actionUserRequest" data-id="<?php echo $request['request_id']; ?>" rel="reject" id="reject_<?php echo $request['request_id'];?>_<?php echo $request['requestProcess_id'];?>" title="Reject"><li class="icon-remove"></li></button-->
                    </span>
                </div>
            </div>
        <?php
            }
        if($userLevel==1){
            foreach($queryRegRequest as $regRequest){
            ?>
                <div class="regRequest" id="notiLi" style="cursor: pointer">
                    <div style="float: left;color: #000;margin-left: 5px;">
                        <?php echo myAvatar($regRequest['extension'],$regRequest['id'],50,50,"none"); ?>
                        <div style="color: #000;margin-left: 5px;font-weight: bold;float: left"><?php echo getUserWholeName($regRequest['id']);?></div>
                        <div style="color: rgb(121, 121, 121);margin-left: 5px;float: left;font-size: 10px;font-family: sans-serif"><?php echo $regRequest['email'];?></div>
                    </div>
                </div>
            <?php
            }
        }
        
        ?>
        <div style="float: left;min-height: 20px;width: 100%;font-size: 10px;color: #000;margin-top: 5px;text-align: center;font-weight: bold;">
            VIEW ALL
        </div>
        <script>
            jQuery("label.timeago").timeago();  
        </script>
        <?php
        
    }else if($_POST['action']=="getButtons"){
        ?>
        <option value="0">--Select--</option>
        <?php
        $getButtons = $db->query("SELECT * FROM tbbuttons where company_id='$company_id' and is_active=1","array");
        foreach($getButtons as $setButtons){
          ?>
          <option value="<?php echo $setButtons['id']; ?>" id="approverButton_<?php echo $setButtons['id']; ?>"><?php echo $setButtons['button']; ?></option>
          <?php  
        }
        ?>
        <?php
    }else if($_POST['action']=="getDefaultButton"){
        ?>
        <option value="0">--Select--</option>
        <?php
        $getButtons = $db->query("SELECT * FROM tbbuttons where company_id='$company_id' and is_active=1","array");
        foreach($getButtons as $setButtons){
          ?>
          <option value="<?php echo $setButtons['id']; ?>" id="approverButton_<?php echo $setButtons['id']; ?>"><?php echo $setButtons['button']; ?></option>
          <?php  
        }
        ?>
        <?php
    }else if($_POST['action']=="getButtonChoices"){
        $buttonArray = $_POST['buttonProperties'];
        $getButtons = $db->query("SELECT * FROM tbbuttons where company_id='$company_id' and is_active=1","array");
        $getButtonsCount = $db->query("SELECT * FROM tbbuttons where company_id='$company_id' and is_active=1","numrows");
        //store to array the id of buttons
        $arrayDefaultButton = array();
        foreach($getButtons as  $setButtons){
            array_push($arrayDefaultButton,$setButtons['id']);
        }
        //print_r($arrayDefaultButton);
        $arrayUseButton = array();
        $arraySplit = substr($buttonArray,0,strlen($buttonArray)-1);
        $arraySplit = explode("+",$arraySplit);
        for($a=0;$a<count($arraySplit);$a++){
            $buttonID =  explode(":",$arraySplit[$a]);
            $buttonID = $buttonID[0];
            unset($arrayDefaultButton[array_search($buttonID,$arrayDefaultButton)]);
        }
        ?>
        <option value="0">--Select--</option>
        <?php
        for($y=0;$y<$getButtonsCount;$y++){
            if($arrayDefaultButton[$y]!=""){
                $getButtonToShowDB = $db->query("SELECT * FROM tbbuttons where id='".$arrayDefaultButton[$y]."' and is_active = 1 and company_id ='$company_id'","row");
                ?>
                <option value="<?php echo $getButtonToShowDB['id']; ?>" id="approverButton_<?php echo $getButtonToShowDB['id']; ?>"><?php echo $getButtonToShowDB['button']; ?></option>
                <?php
            }
        }
    }else if($_POST['action']=="getButtonProperties"){
        $buttonArray = $_POST['buttonProperties'];
        $arraySplit = substr($buttonArray,0,strlen($buttonArray)-1);
        $arraySplit = explode("+",$arraySplit);
        for($a=0;$a<count($arraySplit);$a++){
             $buttonID =  explode(":",$arraySplit[$a]);
             $nextApprover = explode(">",$buttonID[1]);
             $requestStatus = $nextApprover[1];
             $buttonID = $buttonID[0];
             $nextApprover = $nextApprover[0];
             $getButton = $db->query("SELECT * FROM tbbuttons where id = $buttonID","row");
             
            ?>
            <div class="approverButtonBlock" id="approverButtonBlock_<?php echo $buttonID ?>">
                <span class="fontGray" style="float:left;margin-left:5px;margin-top:10px;font-weight: bold;"><?php echo $getButton['button']; ?></span>
                <span class="pull-right deleteApproverButton" id="deleteApproverButtonapproverButton_<?php echo $buttonID ?>" style="margin-right:5px;margin-top:10px;cursor:pointer">
                    <li class="icon-remove"></li>
                </span>
                <div class="fields">
                    <div class="label fontGray" id="labelObject">Next Processor:</div>
                    <div class="input_position" style="margin-top:5px; border-bottom: none;">
                        <select class="input-x-medium buttonApprover buttonApprover_<?php echo $buttonID ?>" id="buttonApprover_<?php echo $buttonID ?>">
                            <option value="0" <?php echo setSelected(0,$nextApprover) ?>>--Select--</option>
                            <option value="1" <?php echo setSelected(1,$nextApprover) ?>>Back to Requestor</option>
                            <option value="3" <?php echo setSelected(3,$nextApprover) ?>>Next Processor</option>
                            <?php
                            $appendOption = $_POST['appendOption'];
                            $arraySplitBtn = explode(",",$appendOption);
                            for($b=0;$b<count($arraySplitBtn);$b++){
                                if($arraySplitBtn[$b]!=""){
                                    ?>
                                    <option value="<?php echo $arraySplitBtn[$b]; ?>" <?php echo setSelected($arraySplitBtn[$b],$nextApprover); ?>><?php echo $arraySplitBtn[$b]; ?></option>
                                    <?php   
                                }
                            }
                            echo $_POST['node_id'];
                            ?>
                            <option value="2" <?php echo setSelected(2,$nextApprover) ?>>Approved</option></select>
                        </select>
                    </div>
                </div>
                <div class="fields requestStatusLabel_<?php echo $buttonID ?> <?php echo setDisplayPerButton($nextApprover);?>">
                    <div class="label fontGray" id="labelObject">Request Status:</div>
                    <div class="input_position" style="margin-top:5px; border-bottom: none;">
                        <select class="input-x-medium requestStatus requestStatus_<?php echo $buttonID ?>" id="requestStatus_<?php echo $buttonID ?>">
                            <option value="2" <?php echo setSelected(2,$requestStatus) ?>>Reject</option>
                            <option value="4" <?php echo setSelected(4,$requestStatus) ?>>Draft</option>
                        </select>
                    </div>
                </div>
            </div>
            <br />
            <?php
        }
        
        /*
         *<div class="approverButtonBlock" id="approverButtonBlock_'+approverButtonVal+'"><span class="fontGray" style="margin-left:5px;margin-top:10px;font-weight: bold;">'+approverButtonText+'</span><span class="pull-right deleteApproverButton" id="deleteApproverButton'+approverButton+'" style="margin-right:5px;margin-top:10px;cursor:pointer"><li class="icon-remove"></li></span>'+
                                                '<div class="fields">'+
                                                    '<div class="label fontGray" id="labelObject">Next Approver:</div>'+
                                                    '<div class="input_position" style="margin-top:5px; border-bottom: none;">'+
                                                    '<select class="input-x-medium buttonApprover buttonApprover_'+approverButtonVal+'" id="buttonApprover_'+approverButtonVal+'">'+
                                                    '<option value="0">--Select--</option><option value="1">Back to Requestor</option><option value="3">Next Approver</option><option value="2">Approved</option></select>'+
                                                    '</div>'+
                                                    '<div class="label fontGray" id="labelObject">Request Status:</div>'+
                                                    '<div class="input_position" style="margin-top:5px; border-bottom: none;">'+
                                                    '<select class="input-x-medium requestStatus requestStatus_'+approverButtonVal+'" id="requestStatus_'+approverButtonVal+'">'+
                                                    '<option value="">--Select--</option>'+
                                                    '<option value="0">Pending</option>'+
                                                    '<option value="2">Reject</option>'+
                                                    '<option value="4">Draft</option>'+
                                                    '<option value="1">Approved</option></select>'+
                                                    '</div>'+
                                                '</div></div><br />
        */
    }else if($_POST['action']=="countTooltipVal"){
        $role = $_POST['role'];
        $count = $db->query("SELECT * FROM tbuser where role='$role' and role_type = 1","numrows");
        echo $count;
    }else if($_POST['action']=="getTooltip"){
        $role = $_POST['role'];
        $getHead = $db->query("SELECT * FROM tbuser where role='$role' and role_type = 1 and company_id='$company_id'","row");
        echo myAvatar($getHead['extension'],$getHead['id'],50,50,"none")."
        <span style='font-family: sans-serif;font-size:10px;color: rgb(121, 121, 121);'>
        <br />

        ".getUserWholeName($getHead['id'])."
        </span>";
    }else if($_POST['action']=="getTooltipWorkflow"){
        ?>
        <div style="font-size: 10px">
            Processor: <?php echo $_POST['approver']; ?><br />
            Status: <?php echo $_POST['approverStatus']; ?><br />
            Buttons:<br />
            <?php
            $buttons = explode("+",$_POST['buttons']);
            for($a=0;$a<count($buttons)-1;$a++){
                $buttonID =  explode(":",$buttons[$a]);
                $nextApprover = explode(">",$buttonID[1]);
                $requestStatus = $nextApprover[1];
                $buttonID = $buttonID[0];
                $nextApprover = $nextApprover[0];
                $getButton = $db->query("SELECT * FROM tbbuttons where id = ".$buttonID[0]."","row");
                echo "-".$getButton['button']."<br />";
            }
            //echo "<div style='float:left; width:100%'>".substr($buttons,0,strlen($buttons)-2)."</div>";
            ?>
            
        </div>
        <?php
    }else if($_POST['action']=="getTooltipWorkflowUpdate"){
        echo $_POST['wf_id'];
    }else if($_POST['updateOnlineStatus']){
        $set = array("online"=>0);
        $con = array("id"=>$user_id);
        $db->update("tbuser",$set,$con);
    }else if($_POST['action']=="add_button"){
        //echo "button Added";
        $value = $_POST['value'];
        if(!empty($value)){
            $insert = array(
                "company_id"=>$company_id,
                "button"=>$value,
                "image_path"=>"",
                "is_active"=>1
            );
            $db->insert("tbbuttons",$insert);
        }else{
            echo "error";
        }
    }else if($_POST['action']=="add_app_status"){
        //echo "approver status Added";
        $value = $_POST['value'];
        if(!empty($value)){
            $insert = array(
                "company_id"=>$company_id,
                "approverStatus"=>$value,
                "is_active"=>1
            );
            echo $db->insert("tbapproverstatus",$insert);
        }else{
            echo "error";
        }
    /*******FOR SYSTEM SETTINGS(UPDATE AND DELETE)***************/
    }else if($_POST['action']=="updateSystemFields"){
        $type = $_POST['type'];
        $value = $_POST['value'];
        $id = $_POST['id'];
        
        if($type=="position"){
            $update = array(
                "position"=>$value
            );
            $where = array(
                "id"=>$id,
                "company_id"=>$company_id
            );
            echo $db->update("tbposition",$update,$where);
        }else if($type=="approverStatus"){
            $update = array(
                "approverStatus"=>$value
            );
            $where = array(
                "id"=>$id,
                "company_id"=>$company_id
            );
            $db->update("tbapproverstatus",$update,$where);
        }else if($type=="button"){
            $update = array(
                "button"=>$value
            );
            $where = array(
                "id"=>$id,
                "company_id"=>$company_id
            );
            $db->update("tbbuttons",$update,$where);
        }
    }else if($_POST['action']=="deleteSystemFields"){ //deactivate
        $type = $_POST['type'];
        $id = $_POST['id'];
        if($type=="position"){
            $update = array(
                "is_active"=>0
            );
            $where = array(
                "id"=>$id,
                "company_id"=>$company_id
            );
            echo $db->update("tbposition",$update,$where);
        }else if($type=="approverStatus"){
            $update = array(
                "is_active"=>0
            );
            $where = array(
                "id"=>$id,
                "company_id"=>$company_id
            );
            $db->update("tbapproverstatus",$update,$where);
        }else if($type=="button"){
            $update = array(
                "is_active"=>0
            );
            $where = array(
                "id"=>$id,
                "company_id"=>$company_id
            );
            $db->update("tbbuttons",$update,$where);
        }
    }else if($_POST['action']=="updateLogout"){
        $update =array(
            "online"=>0
        );
        $where = array(
            "id"=>$user_id
        );
        $db->update("tbuser",$update,$where);
    }
    
    
}
?>